<?php
	
	class LoginHandler{
	
		private $isUserLoggedIn = "isUserLoggedIn";
		
		// Function to check if the user is logged in
		// Returns true if the user is logged in, and false if it isn't.
		function IsLoggedIn(){
			if (isset($_SESSION[$this->isUserLoggedIn])){
				if ($_SESSION[$this->isUserLoggedIn] == true) {
					return true;				
				}
			}
	 	 	return false;
			
		}
		
		// Function to log the user in. 
		// Returns true is successful, or false if it failed.
		function DoLogin($username,$password){
			if($username == 'correctUsername' && $password == 'correctPassword'){
				$_SESSION[$this->isUserLoggedIn] = true;
				return true;
			}
			return false;
		}
		
		// Function to log out the user.
		// Returns nothing.
		function DoLogout(){
			$_SESSION[$this->isUserLoggedIn] = false;
		}
		
		//Function to test the LoginHandler class
		//Returns true if succesfull, throws Exception if anything fails
		function Test(){
			
			$this->DoLogout();
			if($this->IsLoggedIn() == true)
			{
				throw new Exception("IsLoggedIn or DoLogout is not working as intended.");
				return false;
			}
			
			if($this->DoLogin('badUsername','badPassword') == true)
			{
				throw new Exception("DoLogin is not working as intended. Able to log in with incorrect user data");
				return false;
			}
			
			if($this->DoLogin('correctUsername','correctPassword') == false)
			{
				throw new Exception("DoLogin is not working as intended. Unable to log in with correct user data");
				return false;
			}
			
			if($this->IsLoggedIn() == false)
			{
				throw new Exception("IsLoggedIn is not working as intended.");
				return false;
			}
			
			$this->DoLogout();
			if($this->IsLoggedIn() == true)
			{
				throw new Exception("DoLogout is not working as intended.");
				return false;
			}
			
			if($this->DoLogin('correctUsername', 'badPassword') == true)
			{
				throw new Exception("DoLogin is not working as intended. Able to log in with incorrect user data");
				return false;
			}
			if($this->DoLogin('badUsername', 'correctPassword') == true)
			{
				throw new Exception("DoLogin is not working as intended. Able to log in with incorrect user data");
				return false;
			}
			
			return true;
		}
	}
	